package com.lc.luzhengtang.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lc.luzhengtang.R;
import com.lc.luzhengtang.view.CheckView;
import com.zcx.helper.adapter.AppCarAdapter;
import com.zcx.helper.adapter.AppRecyclerAdapter;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.glide.GlideLoader;
import com.zcx.helper.view.AppCheck;


/**
 * Created by Administrator on 2016/10/12.
 */
public class ShopCarAdapter2 extends AppCarAdapter {

    public ShopCarAdapter2(Context context) {

        super(context);

        addItemHolder(Shop.class, ShopHolder.class);

        addItemHolder(Good.class, GoodHolder.class);

        addItemHolder(Bottom.class, BottomHolder.class);

    }

    public static class ShopHolder extends AppCarAdapter.ShopViewHolder<Shop> {

        @BoundView(R.id.item_shopping_car_good_select) private View select;

        @BoundView(R.id.item_shopping_car_good_check) private CheckView check;

        @BoundView(R.id.item_shopping_car_shop_title) private TextView title;

        public ShopHolder(AppRecyclerAdapter adapter, Context context, View view) {

            super(adapter, context, view);

        }

        @Override
        public int resourceId() {

            return R.layout.item_shopping_car_shop;

        }

        @Override
        public void load(Context context, final AppCarAdapter appCarAdapter, final Shop shop, boolean b) {

            select.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    check.setCheck(!check.isCheck());

                }

            });

            check.setOnCheckChangeListener(new AppCheck.OnCheckChangeListener() {

                @Override
                public void onCheckChange(View view, boolean b) {

                    appCarAdapter.shopSelect(shop, b);

                }

            });

            check.setCheck(b ? shop.isDelete : shop.isSelect, false);

            title.setText(shop.title);

        }

    }

    public static class GoodHolder extends AppCarAdapter.GoodViewHolder<Good>{

        @BoundView(R.id.item_shopping_car_good_select) private View select;

        @BoundView(R.id.item_shopping_car_good_check) private CheckView check;

        @BoundView(R.id.item_shopping_car_good_name) private TextView name;

        @BoundView(R.id.item_shopping_car_good_price) private TextView price;

        @BoundView(R.id.item_shopping_car_good_content) private TextView content;

        @BoundView(R.id.item_shopping_car_good_delete) private ImageView delete;

        @BoundView(R.id.item_shopping_car_good_image) private ImageView image;

        @BoundView(R.id.item_shopping_car_good_number) private TextView number;

        @BoundView(R.id.item_shopping_car_good_add) private TextView add;

        public GoodHolder(AppRecyclerAdapter adapter, Context context, View view) {

            super(adapter, context, view);

        }

        @Override
        public int resourceId() {

            return R.layout.item_shopping_car_good;

        }

        @Override
        public void load(Context context, final AppCarAdapter appCarAdapter, final Good good, boolean b) {

            add.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    good.number ++;

                    appCarAdapter.accounting();

                }

            });

            select.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    check.setCheck(!check.isCheck());

                }

            });

            delete.setVisibility(b ? View.INVISIBLE : View.VISIBLE);

            delete.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    appCarAdapter.deleteGood(good);

                }

            });

            check.setOnCheckChangeListener(new AppCheck.OnCheckChangeListener() {

                @Override
                public void onCheckChange(View view, boolean b) {

                    appCarAdapter.goodSelect(good, b);

                }

            });

            GlideLoader.getInstance().get(context, good.image, image);

            check.setCheck(b ? good.isDelete : good.isSelect, false);

            name.setText(good.name);

            content.setText(good.content);

            price.setText("￥" + good.price);

            number.setText("x" + good.number);

        }
    }

    public static class BottomHolder extends AppCarAdapter.BottomViewHolder<Bottom>{

        @BoundView(R.id.item_shopping_car_shop_title) private TextView title;

        public BottomHolder(AppRecyclerAdapter adapter, Context context, View view) {

            super(adapter, context, view);

        }

        @Override
        public int resourceId() {

            return R.layout.item_shopping_car_bottom;

        }

        @Override
        public void load(Context context, AppCarAdapter appCarAdapter, Bottom bottom, boolean b) {

            title.setText("合计:" + bottom.total);

        }

    }

    public static class Shop extends AppCarAdapter.ShopItem{

        public String title;

    }

    public static class Good extends AppCarAdapter.GoodItem{

        public String name, content, image;

        public Good(ShopItem shopItem) {

            super(shopItem);

        }

    }

    public static class Bottom extends AppCarAdapter.BottomItem{

        public Bottom(ShopItem shopItem) {

            super(shopItem);

        }

    }

}
